import { defineStore } from 'pinia'
import type { IAuthState, IMenuTagData } from './type'
import router from '@/router'
import { DefaultRouterName } from '@/config/global'

export const useAuthStore = defineStore('auth', {
  state: (): IAuthState => ({
    menuList: [], // 菜单列表
    btnList: [], // 按钮列表
    sideList: [], // 左侧菜单列表
    sideBasePath: '', // 左侧菜单基础路径
    tagList: [], // 页签列表
    showList: [], // 实际展示的页签
    moreList: [] // 收入"更多"中的页签
  }),
  getters: {
    // 过滤掉不需要显示的菜单
    filterMenuList(state) {
      const filterMenus = state.menuList.filter((item) => item.meta?.hideMenu !== true)

      return filterMenus
    }
  },
  actions: {
    /**
     * 添加tagList
     */
    addTagList(tagData: IMenuTagData) {
      let findIndex = -1
      const tagLen = this.tagList.length
      for (let i = 0; i < tagLen; i++) {
        if (this.tagList[i].path == tagData.path) {
          findIndex = i
          break
        }
      }
      // 如果tag已存在则修改名字
      if (findIndex !== -1) {
        this.tagList[findIndex].label = tagData.label
        // 如果在moreList中，则将其移动到tabList最后一位
        if (this.moreList.find((item) => item.path == tagData.path)) {
          this.tagList.splice(findIndex, 1)
          this.tagList.push(tagData)
        }
      }
      // 如果不存在则添加
      else {
        this.tagList.push(tagData)
      }
    },
    /**
     * 移除指定path的页签
     * @param path 删除的tag地址
     * @param isActive 删除的是否是激活中的tag
     */
    removeTag(path: string, isActive: boolean) {
      this.tagList = this.tagList.filter((item) => item.path != path)
      if (this.tagList.length <= 0) {
        setTimeout(() => {
          router.push({ name: DefaultRouterName })
        }, 0)
      }
      // 删除的如果是激活中的tag，则跳转到最后一个tag页
      else if (isActive) {
        setTimeout(() => {
          router.push(this.tagList[this.tagList.length - 1].fullPath)
        }, 0)
      }
    },
    /**
     * 关闭所有tag
     */
    removeAllTags() {
      this.tagList = this.tagList.filter((item) => item.name == DefaultRouterName)
      setTimeout(() => {
        router.push({ name: DefaultRouterName })
      }, 0)
    },
    /**
     * 关闭其他tag
     */
    removeOtherTags(path: string) {
      this.tagList = this.tagList.filter((item) => item.path == path)
    },
    /**
     * 关闭左侧tag
     */
    removeLeftTags(path: string) {
      let curIndex = -1
      for (let i = 0; i < this.tagList.length; i++) {
        if (this.tagList[i].path == path) {
          curIndex = i
          break
        }
      }
      this.tagList = this.tagList.filter((item, index) => index >= curIndex)
    },
    /**
     * 关闭右侧tag
     */
    removeRightTags(path: string) {
      let curIndex = -1
      for (let i = 0; i < this.tagList.length; i++) {
        if (this.tagList[i].path == path) {
          curIndex = i
          break
        }
      }
      this.tagList = this.tagList.filter((item, index) => index <= curIndex)
    },
    /**
     * 初始化数据
     */
    initData() {
      this.menuList = []
      this.btnList = []
      this.sideList = []
      this.sideBasePath = ''
      this.tagList = []
      this.showList = []
      this.moreList = []
    }
  }
})
